package com.example.demo1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建工厂
        ConnectionFactory factory = new ConnectionFactory();
        //服务地址
        factory.setHost("192.168.1.3");
        //虚拟机
        factory.setVirtualHost("iotVirtual");
        //账号
        factory.setUsername("remote");
        //密码
        factory.setPassword("remote");
        //端口
        factory.setPort(5673);
        //创建连接
        Connection connection = factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("消费者1号：" + new String(message.getBody()));
        };

        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("消息消费被中断");
        };
        //消费消息
        //1、消费哪个队列
        //2、消费成功之后是否需要自动应答
        //3、接收消息
        channel.basicConsume("queue_name", true, deliverCallback, cancelCallback);
    }
}
